package com.koi.mqttdemo;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.koi.mqttdemo.inteface.IMqttGetMsgCallBack;
import com.koi.mqttdemo.service.MqttConnectionService;

import org.eclipse.paho.android.service.MqttAndroidClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements IMqttGetMsgCallBack {

    public static Context mContext;
    // 电灯泡按钮，点击会发布内容。发布的内容有灯泡的状态决定的。
    // mIsLight 表示灯泡当前的状态是否为亮
    private Button mBulbButton;
    private boolean mIsLight = false;

    private ListView mListView;
    // 行列表。一行中包含一个图标，一个标题和一个数值
    private ArrayList mListViewItemList;
    private SimpleAdapter mSimpleAdapter;
    private String mListItemDates[] = {"0", "0", "0"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        /**
         * 灯泡按钮的点击事件
         */
        mBulbButton = findViewById(R.id.button);
        mBulbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当前的灯泡的状态
                if (mIsLight) {
                    mBulbButton.setBackgroundResource(R.mipmap.light);
                    Mqtt mqtt = MqttConnectionService.getMqttInstance();
                    mqtt.sendTopic("the Bulb status", "亮");
                } else {
                    mBulbButton.setBackgroundResource(R.mipmap.light3);
                    Mqtt mqtt = MqttConnectionService.getMqttInstance();
                    mqtt.sendTopic("the Bulb status", "灭");
                }
                mIsLight = !mIsLight;
            }
        });

        mListView = findViewById(R.id.listview);
        initListViewData();
        String mapKeys[] = {"img", "title", "content"};
        int viewRid[] = {R.id.listitem_image, R.id.listitem_title, R.id.listitem_content};
        mSimpleAdapter = new SimpleAdapter(this, mListViewItemList, R.layout.listview_item, mapKeys, viewRid);
        mListView.setAdapter(mSimpleAdapter);

        Intent startIntent=new Intent(this,MqttConnectionService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            this.startForegroundService(startIntent);
        }
        startService(startIntent);

        MqttConnectionService.setMqttReceiveCallBack(MainActivity.this);
    }

    // 初始化列表的数据。
    // image：图标显示
    // title：标题
    // content: 内容，订阅的数值
    private void initListViewData() {
        // 图标图片资源ID
        int imagesRid[] = {R.mipmap.weathy, R.mipmap.tem, R.mipmap.hum};
        String titles[] = {"光敏","温度","湿度"};
        String contents[] = {"0", "0", "0"};
        // 将内容更新到ListArray中
        mListViewItemList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imagesRid.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("img", imagesRid[i]);
            map.put("title", titles[i]);
            map.put("content", contents[i]);
            mListViewItemList.add(map);
        }
    }

    // 通过接受到的信息，更新列表信息

    private void updateListViewDate() {
        // 1.清空列表
        mListViewItemList.clear();

        // 2.重新填上列表信息，contents改为保存的数值信息
        // 图标图片资源ID
        int imagesRid[] = {R.mipmap.weathy, R.mipmap.tem, R.mipmap.hum};
        String titles[] = {"光敏","温度","湿度"};
        // 将内容更新到ListArray中
        //mListViewItemList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imagesRid.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("img", imagesRid[i]);
            map.put("title", titles[i]);
            map.put("content", mListItemDates[i]);
            mListViewItemList.add(map);
        }

        // 3.通知适配器数据更新
        mSimpleAdapter.notifyDataSetChanged();
    }

    @Override
    public void setWenDuMsg(String wendu) {
        mListItemDates[1] = wendu;
        updateListViewDate();
    }

    @Override
    public void setguangminMsg(String guangmin) {
        mListItemDates[0] = guangmin;
        updateListViewDate();
    }

    @Override
    public void setShiDuMsg(String ShiDu) {
        mListItemDates[2] = ShiDu;
        updateListViewDate();
    }
}